<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;


class Image extends Controller{
    //上传文件 返回压缩地址
    public function upload(){
        $file = Request::instance()->file('file');
        if ($file){
            $path = ROOT_PATH . 'public' . DS . 'upload';
            $info = $file->move($path);
            if ($info && $info->getPathname()) {
                $imagePath = $info->getSaveName();
                $image = \think\Image::open($path.'/'.$imagePath);
                $date_path_real = $path.'/'.date('Ymd');
                $date_path = date('Ymd');
                if(!file_exists($date_path_real)){
                    mkdir($date_path,0777,true);
                }
                $thumb_real_path = $date_path_real.'/'.'thumb_'.$info->getFilename();
                $thumb_path = $date_path.'/'.'thumb_'.$info->getFilename();
                $image->thumb(300, 300, \think\Image::THUMB_SCALING)->save($thumb_real_path);
                return success('success',[ 'path'=>$thumb_path]);
                //return success('success',[ 'path'=>$thumb_path]);//缩略图
            }
        }
        return error('upload error!');
    }

    //上传文件 返回不压缩地址
    public function uploadNo(){
        $file = Request::instance()->file('file');
        if ($file){
            $path = ROOT_PATH . 'public' . DS . 'upload';
            $info = $file->move($path);
            if ($info && $info->getPathname()) {
                $imagePath = $info->getSaveName();
                return success('success',['path'=>$imagePath]);
            }
        }
        return error('upload error!');
    }

    public function wangUpload(){
        $file = Request::instance()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $data = [];
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if ($info) {
                $imagePath = str_replace('\\','/',$info->getSaveName());
                $data = [
                    'code' => 1,
                    'msg' => '上传成功',
                    //'data' => !empty($info) ? ['/public/uploads/'.$imgp] : []
                    'data' => [config('setting.domain_prefix').config('setting.image_prefix').$imagePath]
                ];

            } else {
                $data = [
                    'code' => 0,
                    'msg' => '上传失败',
                    //'errno' => $file->getError(),
                    'data' => []
                ];

            }
        }else{
            $data = [
                'code'=>0,
                'msg' => '上传失败',
            ];
        }
        return json_encode($data);
    }


    //上传文件

    /**
    {
    "code": 0 //0表示成功，其它失败
    ,"msg": "" //提示信息 //一般上传失败后返回
    ,"data": {
    "src": "图片路径"
    ,"title": "图片名称" //可选
    }
    }
     */
    public function layUpload(){
        $file = Request::instance()->file('file');
        if ($file){
            $path = ROOT_PATH . 'public' . DS . 'upload';
            $info = $file->move($path);
            if ($info && $info->getPathname()) {
                /**
                #info: array:5 [
                "name" => "微信图片_20221007102307 - 副本 - 副本 (2).jpg"
                "type" => "image/jpeg"
                "tmp_name" => "/tmp/phpMQXet0"
                "error" => 0
                "size" => 226245
                ]
                 */
                $imagePath = $info->getSaveName();
                $realPath = $path.'/'.$imagePath;

                $sizeaa =  getimagesize ($realPath);
                if($sizeaa[0] > 375){
                    $width_tmp = $sizeaa[0];
                    $height_tmp = $sizeaa[1];
                    $width = 375;
                    $height = round($width * $height_tmp / $width_tmp);
                    $image = \think\Image::open($realPath);
                    // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                    $image->thumb($width, $height)->save($realPath);
                }

                return
                    [
                        'code'=>0,
                        'msg' => '上传成功',
                        'data' => [
                            'src' => config('setting.domain_prefix').config('setting.image_prefix').$imagePath,
                            'title' => 'title'
                        ]
                    ];
            }
        }
        return
            [
                'code'=>1,
                'msg' => '上传失败',
            ];
    }


    public function layUploadNo(){
        $file = Request::instance()->file('file');
        if ($file){
            $path = ROOT_PATH . 'public' . DS . 'upload';
            $info = $file->move($path);
            if ($info && $info->getPathname()) {
                /**
                #info: array:5 [
                "name" => "微信图片_20221007102307 - 副本 - 副本 (2).jpg"
                "type" => "image/jpeg"
                "tmp_name" => "/tmp/phpMQXet0"
                "error" => 0
                "size" => 226245
                ]
                 */
                $imagePath = $info->getSaveName();
                return
                    [
                        'code'=>0,
                        'msg' => '上传成功',
                        'data' => [
                            'src' => config('setting.domain_prefix').config('setting.image_prefix').$imagePath,
                            'title' => 'title'
                        ]
                    ];
            }
        }
        return
            [
                'code'=>1,
                'msg' => '上传失败',
            ];
    }
}
